package com.dx.weichat.weichat.service;

import com.dx.weichat.weichat.entity.CustomEventMsg;
import com.dx.weichat.weichat.entity.LocationEventMsg;
import com.dx.weichat.weichat.entity.MsgType;
import com.dx.weichat.weichat.entity.TextMsg;
import com.dx.weichat.weichat.redisUtils.RedisUtil;
import com.dx.weichat.weichat.wechatUtils.JsonUtil;
import com.google.gson.Gson;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: dx
 * @date: 2018/10/7/0007
 * @description:
 */
@Service
public class WXMessageHandleService {
	@Autowired
	private CommonMsgService commonMsgService;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private EventMsgService eventMsgService;
	private static Logger logger = LoggerFactory.getLogger(WXMessageHandleService.class);
	public String handleMessage(HttpServletRequest request, HttpServletResponse response){
		String responseText = "";
		try {
			JSONObject jsonObject = JsonUtil.xml2jsonString(request.getInputStream()).getJSONObject("xml");
			String Msgtype=jsonObject.getString("MsgType");
			String MsgInfo=jsonObject.getString("FromUserName")+jsonObject.get("CreateTime");
			//判断此消息是否已经发过 排重
			//if(redisUtil.get(MsgInfo)==null) {
				//将消息存起来
				//redisUtil.set(MsgInfo,MsgInfo,30);
				switch (Msgtype) {
					case MsgType.TEXT:
						TextMsg receiveTextMsg = new Gson().fromJson(jsonObject.toString(), TextMsg.class);
						logger.info("接收到文本信息:{}", receiveTextMsg.getContent());
						logger.info("------------------------------开始处理文本类型消息-------------------------------------");
						responseText = commonMsgService.processTextMsg(receiveTextMsg);
						logger.info("------------------------------文本类型消息处理完毕-------------------------------------");
						break;
					case MsgType.Event:
						String eventKey = jsonObject.getString("Event");
						switch (eventKey) {
							case MsgType.CUSTMOEVENT:
								logger.info("------------------------------开始处理自定义菜单事件-------------------------------------");
								CustomEventMsg customEventMsg = new Gson().fromJson(jsonObject.toString(), CustomEventMsg.class);
								responseText = eventMsgService.processCustomMsg(customEventMsg);
								logger.info("------------------------------自定义菜单事件处理完毕-------------------------------------");
								break;
							case MsgType.LOCATIONEVENT:
								logger.info("------------------------------开始处理位置类型事件-------------------------------------");
								LocationEventMsg locationEventMsg = new Gson().fromJson(jsonObject.toString(), LocationEventMsg.class);
								//将位置保存进redis
								redisUtil.set(locationEventMsg.getFromUserName(), locationEventMsg);
								logger.info("------------------------------位置类型事件处理完毕-------------------------------------");
								break;
						}
						break;
					default:
						logger.error("未识别的消息类型");
						break;
				}
			//}
		}catch (Exception e){
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return responseText;
	}
}
